<template>
	<view class="content">
		<view class="title">
			<text class="iconfont icon-shop"></text>
			<text>优购生活馆</text>
		</view>
		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="goods-item" v-for="(item, index) in goodsList" :key="index" @click="toItem(item.goods_id)">
				<text class="iconfont" :class="item.checked ? 'icon-icon-checked':'icon-unchecked'" @click.stop="toggleChecked(item)"></text>
				<image :src="item.goods_small_logo" alt="" />
				<view class="right">
					<text class="text-line2">{{item.goods_name}}</text>
					<view class="btm">
						<text class="price">
							￥
							<text>{{item.goods_price}}</text>
							.00
						</text>
						<view class="goods-num">
							<button @click.stop="subNum(item,index)">-</button>
							<text>{{item.num}}</text>
							<button @click.stop="addNum(item)">+</button>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="account">
			<view class="select-all" @click="toggleAllChecked">
				<text class="iconfont " :class="isAll?'icon-icon-checked':'icon-unchecked'"></text>
				<text>全选</text>
			</view>
			<view class="price-wrapper">
				<view class="price">
					<view>
						合计:
						<text class="num">￥{{totalSum}}.00</text>
					</view>
					<text class="info">包含运费</text>
				</view>
			</view>
			<view class="account-btn" @click="toPay">结算({{totalNum}})</view>
		</view>
	</view>
</template>
<script>
	export default {
		computed: {
			// 是否全选
			isAll: {
				get() {
					// 全部商品勾选 就 勾选
					return this.goodsList.every(item => {
						return item.checked
					})
				},
				set(status) {
					this.goodsList.forEach(item => {
						item.checked = status
					})
				}
			},
			// 购物车商品总数量
			totalNum() {
				return this.goodsList.reduce((num, item) => {
					return num + (item.checked ? item.num : 0)
				}, 0)
			},
			//购物车商品总价
			totalSum() {
				return this.goodsList.reduce((num, item) => {
					return num + (item.checked ? (item.goods_price * item.num) : 0)
				}, 0)
			},
		},
		watch: {
			// goodsList更新到storage
			goodsList: {
				handler() {
					console.log(111, this.goodsList)
					let cart = this.goodsList.map(item => {
						return {
							goods_id: item.goods_id,
							checked: item.checked,
							num: item.num
						}
					})
					uni.setStorageSync('cart', cart)
				},
				deep: true
			}
		},
		data() {
			return {
				goodsList: []
			}
		},
		onShow() {
			this.cartArr = uni.getStorageSync('cart') || []
			let goodsIds = this.cartArr.map(item => {
				return item.goods_id
			})

			goodsIds = goodsIds.join(',')

			this.getCartGoodsList(goodsIds)
		},
		methods: {
			// 去结算
			toPay() {
				// 遍历购物车，只要有一个商品勾选了就为true
				let isChecked = this.goodsList.some(item => {
					return item.checked
				})
				// 购物车为空时|| 未选择商品
				if (this.totalNum === 0 || !isChecked) {
					uni.showToast({
						title: '请先选择商品',
						icon: 'none'
					})
					return
				}
				let token = uni.getStorageSync('token')
				if (!token) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return
				}
				uni.navigateTo({
					url: '/pages/pay/pay'
				})
			},
			// 全选状态
			toggleAllChecked() {
				this.isAll = !this.isAll
			},
			// 减
			subNum(item, index) {
				if (item.num === 1) {
					uni.showModal({
						title: '提示',
						content: '是否删除该商品',
						success: res => {
							if (res.confirm) {
								this.goodsList.splice(index, 1)
							}
						}
					});
					return
				}
				item.num--
			},
			// 加
			addNum(item) {
				console.log(item)
				item.num++
			},
			// 切换选中状态
			toggleChecked(item) {
				item.checked = !item.checked
			},
			//跳转详情页
			toItem(goods_id) {
				uni.navigateTo({
					url: '/pages/item/item?goods_id=' + goods_id
				})
			},
			// 获取购物车商品列表
			async getCartGoodsList(goodsIds) {
				let _goodsList = await this.$request({
					url: '/api/public/v1/goods/goodslist?goods_ids=' + goodsIds
				})

				this.goodsList = _goodsList.reduce((pre, cur) => {
					let target = pre.find(item => item.goods_id === cur.goods_id)
					Object.assign(target, cur)
					return pre
				}, this.cartArr)
			}
		}
	}
</script>
<style lang="less">
	.iconfont {
		font-size: 44rpx;
	}

	.title {
		height: 88rpx;
		display: flex;
		align-items: center;

		.iconfont {
			color: #999;
			margin: 6rpx 12rpx 0 32rpx;
		}
	}

	.goods-item {
		height: 206rpx;
		background-color: #fff;
		border-top: 1rpx solid #ddd;
		display: flex;
		box-sizing: border-box;
		align-items: center;

		.iconfont {
			color: #eb4450;
			margin: 0 30rpx;
		}

		image {
			width: 160rpx;
			height: 160rpx;
		}

		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			margin: 0 20rpx 0 18rpx;

			.btm {
				display: flex;
				margin-top: 40rpx;
				justify-content: space-between;

				.price {
					color: #eb4450;
					display: flex;

					>text {
						font-size: 24px;
					}
				}

				.goods-num {
					display: flex;
					align-items: center;

					button {
						width: 60rpx;
						height: 50rpx;
						box-sizing: border-box;
						display: flex;
						justify-content: center;
						align-items: center;
						border: 4rpx solid #666;
					}

					text {
						margin: 0 30rpx;
					}
				}
			}
		}
	}

	.goods-list {
		position: absolute;
		bottom: 98rpx;
		top: 88rpx;
		width: 100%;
		overflow: auto;
		padding-bottom: 60rpx;
		background-color: #f4f4f4;
	}

	.account {
		height: 98rpx;
		border-bottom: 1rpx solid #ddd;
		display: flex;
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		align-items: center;

		.iconfont {
			color: #eb4450;
			margin: 0 30rpx;
		}

		.account-btn {
			width: 230rpx;
			height: 98rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			background-color: #eb4450;
		}

		.select-all {
			flex: 1;
			display: flex;
			align-items: center;

			.price-wrapper {
				display: flex;
				flex: 1;
				justify-content: flex-end;
				align-items: center;
			}
		}

		.price {
			margin-right: 20rpx;

			.num {
				color: #eb4450;
			}

			.info {
				color: #999;
			}
		}
	}
</style>
-->
